package com.dindang.propertyfee.app.utils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 70339 on 2018/5/17.
 * 代码无八阿哥
 * 阿弥陀佛
 */
public class TimeComparisonUtils {
    public static int TIME_TIMEOUT = 0;//超时
    public static int TIME_NORMAL = 1;//正常巡检时间
    public static int TIME_IS_NOT = 2;//巡检时间未到

    public static int compareDate(Date dt1, Date endDateTime) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            Date dt2 = endDateTime;
            Date nowDate = new Date();

            if(nowDate.getTime() < dt1.getTime()){
                return TIME_IS_NOT;
            }

            if(nowDate.getTime() > dt1.getTime() && nowDate.getTime() < dt2.getTime()){
                return TIME_NORMAL;
            }

        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return TIME_TIMEOUT;
    }

    public static int getNowDate(Date startDateTime, Date endTime){
//        SimpleDateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd");
//        String endDateTime =dateFormat.format(startDateTime) + " " + endTime;
        return compareDate(startDateTime, endTime);
    }

    public static String getDate(){
        Date nowDate = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String dateString = formatter.format(nowDate);
        return dateString;
    }

    public static String dateTString(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTString2(Date dt1){
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
        String dateString = formatter.format(dt1);
        return dateString;
    }

    public static String dateTMDString(Date dt1){
        Date nowDate = new Date();

        Calendar selectedDate = Calendar.getInstance();//系统当前时间
        SimpleDateFormat formatter = new SimpleDateFormat("MM月dd日");
        String dateString = formatter.format(dt1);
        if(dateTString2(dt1).equals(dateTString2(nowDate))){
            return "今日";
        }
        SimpleDateFormat year = new SimpleDateFormat("yyyy");
        int contrast = selectedDate.get(Calendar.YEAR);

        if(String.valueOf(contrast - 1).equals(year.format(dt1))){
            dateString = "去年 " + dateString;
        }else if(String.valueOf(contrast).equals(year.format(dt1))){
            dateString = "今年 " + dateString;
        }else if(String.valueOf(contrast + 1).equals(year.format(dt1))){
            dateString = "明年 " + dateString;
        }
        return dateString;
    }

    public static Date getStartTime() {
        Calendar todayStart = Calendar.getInstance();
        todayStart.set(Calendar.HOUR_OF_DAY, 0);
        todayStart.set(Calendar.MINUTE, 0);
        todayStart.set(Calendar.SECOND, 0);
        todayStart.set(Calendar.MILLISECOND, 0);
        return todayStart.getTime();
    }

    /**
     * 获取当前月份
     * @return
     */
    public static String getM(){
        SimpleDateFormat formatter = new SimpleDateFormat("M");
        String dateString = formatter.format(new Date());
        return dateString;
    }

    public static Date getEndDateTime(Date startdate, String enddate){
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String endDateTime = dateFormat.format(startdate) + " " + enddate;
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        try {
            return df.parse(endDateTime);
        } catch (Exception exception) {
            exception.printStackTrace();
        }
        return null;
    }

}
